package com.hudongwx.sadpw.fragment.community;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.activity.TieZiDetailActivity;
import com.hudongwx.sadpw.fragment.adapter.MyRecycleAdapter;
import com.hudongwx.sadpw.utils.API;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_tui_jian)
public class TuiJianFragment extends Fragment {
    private View view;
    String TAG="TuiJianFragment";
    MyListAdapter myAdapter;
    MyRecycleAdapterTuiJian adapter;
    JSONArray jsonArray1,jsonArray2;
    @ViewInject(R.id.tuijian_rec1)
    private ListView listView;
    @ViewInject(R.id.tuijian_rec2)
    private RecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null==view){
            view= x.view().inject(this,inflater,container);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
            recyclerView.setLayoutManager(gridLayoutManager);
            adapter=new MyRecycleAdapterTuiJian(jsonArray2);
            recyclerView.setAdapter(adapter);
            myAdapter=new MyListAdapter(jsonArray1);
            listView.setAdapter(myAdapter);
            parseLiang();
            parseShan();
        }
        return view;
    }

    //adapter
    class MyListAdapter extends BaseAdapter{
        public MyListAdapter(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }
        public void updataC1(JSONArray jsonArray){
            this.jsonArray=jsonArray;
            notifyDataSetChanged();
        }
        JSONArray jsonArray;
        @Override
        public int getCount() {
            if (null==jsonArray){
                return 0;
            }
            return jsonArray.length();
        }

        @Override
        public Object getItem(int position) {
            return getItem(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            MyListViewHolderTuiJian myListViewHolderTuiJian=null;
            if (null==jsonArray)return null;
            if (null==convertView){
                myListViewHolderTuiJian=new MyListViewHolderTuiJian();
                convertView=getActivity().getLayoutInflater().inflate(R.layout.community_1_1,null);
                myListViewHolderTuiJian.textView1= (TextView) convertView.findViewById(R.id.tuijian_tv_1);
                myListViewHolderTuiJian.textView2= (TextView) convertView.findViewById(R.id.tuijian_tv_2);
                myListViewHolderTuiJian.imageView= (ImageView) convertView.findViewById(R.id.tuijian_iv_1);
                myListViewHolderTuiJian.linearLayout= (LinearLayout) convertView.findViewById(R.id.tuijian_ll);
                convertView.setTag(myListViewHolderTuiJian);
            }else {
                myListViewHolderTuiJian= (MyListViewHolderTuiJian) convertView.getTag();
            }
            try {
                x.image().bind(myListViewHolderTuiJian.imageView,jsonArray.getJSONObject(position).getString("CoverImg"));
                Log.i(TAG, "getView: image"+myListViewHolderTuiJian.imageView);
                myListViewHolderTuiJian.textView1.setText(jsonArray.getJSONObject(position).getString("Title"));
                myListViewHolderTuiJian.textView2.setText(jsonArray.getJSONObject(position).getString("BoardName"));
                myListViewHolderTuiJian.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(v.getContext(), TieZiDetailActivity.class);
                        try {
                            int id = jsonArray.getJSONObject(position).getInt("Id");
                            intent.putExtra("type",id);
                            getActivity().startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return convertView;
        }
        class MyListViewHolderTuiJian{
            TextView textView1,textView2;
            ImageView imageView;
            LinearLayout linearLayout;
        }
    }
    //解析靓贴
    public void parseLiang(){
        RequestParams params=new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Posts/GetGoodPost");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: data22222222:"+data.toString());
                    myAdapter.updataC1(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    //recycleAdapter
    class MyRecycleAdapterTuiJian extends RecyclerView.Adapter<MyRecycleViewHolderTuiJian>{
        JSONArray jsonArray1;

        public MyRecycleAdapterTuiJian(JSONArray jsonArray1) {
            this.jsonArray1 = jsonArray1;
        }
        public void updateC2(JSONArray jasonArray1){
            this.jsonArray1=jasonArray1;
            notifyDataSetChanged();
        }
        @Override
        public MyRecycleViewHolderTuiJian onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.community_1_2, null);
            return new MyRecycleViewHolderTuiJian(view);
        }

        @Override
        public void onBindViewHolder(MyRecycleViewHolderTuiJian holder, int position) {
            if (null==jsonArray1){
                recyclerView.setVisibility(View.GONE);
            }else {
                try {
                    holder.tv.setText(jsonArray1.getJSONObject(position).getString("Title"));
                    if (jsonArray1.getJSONObject(position).getString("Content").equals("")){
                        holder.textView.setText("");
                        holder.textView.setVisibility(View.GONE);
                    }else{
                        holder.textView.setText(jsonArray1.getJSONObject(position).getString("Content"));
                    }
                    JSONArray array2 = jsonArray1.getJSONObject(position).getJSONArray("ListFile");
                    for (int i = 0; i <array2.length() ; i++) {
                        if (array2.length()==1){
                            x.image().bind(holder.imageView1,array2.getJSONObject(0).getString("Url"));
                        }else if (array2.length()==2){
                            x.image().bind(holder.imageView1,array2.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView2,array2.getJSONObject(1).getString("Url"));
                        }else if (array2.length()>=3){
                            x.image().bind(holder.imageView1,array2.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView2,array2.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView3,array2.getJSONObject(2).getString("Url"));
                        }else if (array2.length()==0){
                            holder.linearLayout.setVisibility(View.GONE);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }

        @Override
        public int getItemCount() {
            if (null==jsonArray1)return 0;
            return jsonArray1.length();
        }
    }
    class MyRecycleViewHolderTuiJian extends RecyclerView.ViewHolder {
        TextView tv,textView;
        LinearLayout linearLayout;
        ImageView imageView1,imageView2,imageView3;
        public MyRecycleViewHolderTuiJian(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.shaidan_tv);
            textView= (TextView) itemView.findViewById(R.id.shaidan_tv_2);
            imageView1= (ImageView) itemView.findViewById(R.id.ll_iv_1);
            imageView2= (ImageView) itemView.findViewById(R.id.ll_iv_2);
            imageView3= (ImageView) itemView.findViewById(R.id.ll_iv_3);
            linearLayout= (LinearLayout) itemView.findViewById(R.id.shandan_ll);
        }
    }
    //解析靓贴
    public void parseShan(){
        RequestParams params=new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/posts");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: data22222222:"+data.toString());
                    adapter.updateC2(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
